//
//  WXCallBack.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/12/11.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import soapp
import RxSwift


public class MOSDK0001:MOSDK{
    public override func begin() {
        WXApi.startLog(by: .detail) { (rn) in
//            print("+++++---OOO\(rn)")
        }
          
        WXApi.registerApp(SDKConfig.WX.appID, universalLink: SDKConfig.WX.universalLink)
        
        print("=----|\(SDKConfig.WX.appID)==|\(SDKConfig.WX.universalLink)")
//        WXApi.checkUniversalLinkReady { (resp, result) in
//            print("|\(result.suggestion)--s--s-000",resp.rawValue,result.success,result.errorInfo)
//        }
        DoPay.share.add(realization: FastWXPay.shared)
        LoginManager.shared.add(engine: WXLoginEngine.shared)
        MoShare.add(plat: WXShare.default)
        HandleOpenURL.add(owner: WXURLOwner.share)
    }
}



public class WXURLOwner:NSObject,WXApiDelegate,MoURLOwnerRealization{
    public var schemes: [String]{
        get{
            return [SDKConfig.WX.appID]
        }set{
            
        }
        
    }
    
    
    public func handleOpenUniversalLink(userActivity: NSUserActivity) -> Bool {
//        print("=====IIIII0----s___WXCokkk")
        return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
    }
    
    static var _share:WXURLOwner? = nil
      static var share:WXURLOwner{
          get{
              guard let share = _share else {
                  let share = WXURLOwner()
                  _share = share
                  return share
              }
              return share
          }
      }
    public func handle(url: URL, resource: String) -> Bool {
        WXApi.handleOpen(url, delegate: self)
    }
    public var hosts: [String] = ["com.tencent.xin"]
    public var plat: String{
           get{
               return "WX"
           }
        
    }

    public static var shared:WXURLOwner = WXURLOwner()
    public override init() {
        super.init()
    }
    
    public let response:PublishSubject<BaseResp> = PublishSubject<BaseResp>()
        
    

    public func onReq(_ req: BaseReq) {
//        print("+++++OIISIIIS--",req)
    }

    public func onResp(_ resp: BaseResp) {
        /*
            WXSuccess           = 0,   成功
            WXErrCodeCommon     = -1,   普通错误类型
            WXErrCodeUserCancel = -2,   用户点击取消并返回
            WXErrCodeSentFail   = -3,    发送失败
            WXErrCodeAuthDeny   = -4,   授权失败
            WXErrCodeUnsupport  = -5,    微信不支持
            */
        
        WXURLOwner.shared.response.onNext(resp)
        if let response = resp as? SendAuthResp{
            WXLoginEngine.shared.handleAuth(respones: response)
            return
        }
        if let response = resp as? SendMessageToWXResp{
            print("分享结果:code:\(response.errCode):\(response.errStr)")

            if response.errCode == 0{
                MoShare.default._finish?(true,"微信分享成功",nil,nil)
//                (_:Bool,_:String,_:MoShare.Media,_:MoShare.Scene)
            }
        }
        if let response = resp as? PayResp{
            
            if let payCode = WXPayCode(rawValue: Int(response.errCode))
            {
                let info = ["msg":payCode.message,"title":payCode.message,"code":response.errCode].mutable_dictionary
                FastWXPay.shared.payCallBack(info: info.mutable_dictionary)
            }
            return
        }
    }
        
  
}


extension JoTask {
    @discardableResult
    public func wxurl(_ url:WXURL) ->JoTask {
        self.url_value = url.url
        return self
    }
}



public enum WXURL:String{
    
    case accessToken = "sns/oauth2/access_token"
    case userinfo = "sns/userinfo"
   
    
    public static var dist_domian:String = "https://api.weixin.qq.com/"
    
    public var url:String{
        get{
            
            return WXURL.dist_domian + self.rawValue
        }
    }
    
}
